

#include "videoTextureActor.h"
#include "renderer.h"

VideoTextureActor::VideoTextureActor(){

    //videoFileName = "resources/videos/MVI_0225.avi";
    loadMovie("resources/video.xml");

    bPlaying = false;
    textureID="videoTexture";
    drawType = DRAW_PLANE;
    registerProperties();

    //player.setLoopState(OF_LOOP_NONE);
}

VideoTextureActor::~VideoTextureActor(){}

void VideoTextureActor::registerProperties(){

    Actor::registerProperties();

}

void VideoTextureActor::setup(){

Actor::setup();
}

void VideoTextureActor::trigger(Actor * other)
{

    if(bPlaying ) player.stop();
    else          player.play();

    bPlaying = !bPlaying;
}

void VideoTextureActor::update(double deltaTime){

    // idle movie
    player.idleMovie();

    // get texture
    GLuint texture = renderer->textureList[textureID]->texture;

    // select our texture
    glBindTexture( GL_TEXTURE_2D, texture );

    // when texture area is small, bilinear filter the closest mipmap
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR );

    // when texture area is large, bilinear filter the first mipmap
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    // if wrap is true, the texture wraps over at the edges (repeat)
    //       ... false, the texture ends at the edges (clamp)
    //glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP );
    //glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP );

    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, player.width, player.height, 0, GL_RGB, GL_UNSIGNED_BYTE, player.getPixels() );

}

void VideoTextureActor::create(){renderer->addActor(this);}


void VideoTextureActor::loadMovie(string fileName){


    TiXmlDocument doc( fileName );
    if (!doc.LoadFile()) return;


    TiXmlHandle hDoc(&doc);
    TiXmlElement * element;
    TiXmlHandle hRoot(0);

    element=hDoc.FirstChildElement().Element();
    // should always have a valid root but handle gracefully if it doesn't
    if (!element) return;
    // save this for later
    hRoot=TiXmlHandle(element);

    element=hRoot.FirstChild( "Video" ).Element();
    string vFileName=element->Attribute("vidFilename");

    cout << "LOADING video file: " << vFileName << endl;
    player.loadMovie(vFileName);
    player.setLoopState(OF_LOOP_NORMAL);
}
